/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jsf.beans.admin;

import JDBCUtil.MyConn;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import manager.PaymenManager;
import model.admin.Payment;

/**
 *
 * @author Danh
 */
@ManagedBean
@RequestScoped
public class PaymentBean {

    private int payment_id;
    private boolean payment_status;
    private String payment_name;
    private Connection conn;

    public PaymentBean() {
    }

    public PaymentBean(int payment_id, boolean payment_status, String payment_name) {
        this.payment_id = payment_id;
        this.payment_status = payment_status;
        this.payment_name = payment_name;
    }

    public int getPayment_id() {
        return payment_id;
    }

    public void setPayment_id(int payment_id) {
        this.payment_id = payment_id;
    }

    public boolean isPayment_status() {
        return payment_status;
    }

    public void setPayment_status(boolean payment_status) {
        this.payment_status = payment_status;
    }

    public String getPayment_name() {
        return payment_name;
    }

    public void setPayment_name(String payment_name) {
        this.payment_name = payment_name;
    }

    public void reset() {
        this.payment_status = false;
        this.payment_name = "";
    }

    public List<Payment> loadPayment() {
        PaymenManager mana = new PaymenManager();
        return mana.getPayment();
    }

    public String editAction(int id, String name, boolean status) {
        this.payment_id = id;
        this.payment_name = name;
        this.payment_status = status;
        return "updatePayment.xhtml";
    }

    public void insertPayment() {
        Payment c = new Payment(this.payment_status, this.payment_name);
        PaymenManager mana = new PaymenManager();
        boolean result = mana.insertPayment(c);
        if (result) {
            loadPayment();
        }
    }

    public String deletePayment(int id) {
        PaymenManager mana = new PaymenManager();
        boolean deletepayment = mana.deletePayment(id);
        if (deletepayment) {
            loadPayment();
        }
        return "";
    }

    public int total_1() {
        int total = 0;
        PreparedStatement pstmt;
        try {
            conn = MyConn.getConnection();
            pstmt = conn.prepareStatement("SELECT * FROM Payment_Details WHERE Payment_Type_Id=1");
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                total++;
            }
        } catch (Exception e) {
        }

        String sql = "SELECT COUNT(P.Payment_Type_Id)as total FROM Payment_Details AS P GROUP BY P.Payment_Type_Id";
        return total;
    }

     public int total_2() {
        int total = 0;
        PreparedStatement pstmt;
        try {
            conn = MyConn.getConnection();
            pstmt = conn.prepareStatement("SELECT * FROM Payment_Details WHERE Payment_Type_Id=2");
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                total++;
            }
        } catch (Exception e) {
        }      
        return total;
    }
     
     public int total_3() {
        int total = 0;
        PreparedStatement pstmt;
        try {
            conn = MyConn.getConnection();
            pstmt = conn.prepareStatement("SELECT * FROM Payment_Details WHERE Payment_Type_Id=3");
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                total++;
            }
        } catch (Exception e) {
        }      
        return total;
    }
     
    public String sua() {
        Payment pr = new Payment(payment_id, payment_status, payment_name);
        PaymenManager mana = new PaymenManager();
        mana.update(pr);
        return "paymentManager.xhtml?faces-redirect=true";
    }
}
